﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Jbe.Composite.Extensions;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Jbe.TestSuite.Infrastructure.Shell.Properties;
using System.Windows.Threading;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Threading;
using System.Globalization;

namespace Jbe.TestSuite.Infrastructure.Shell
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            
#if (DEBUG)
            RunCore();
#else
            RunWithExceptionHandling();
#endif       
        }


        private void RunCore()
        {
            InitializeCultures();
            
            Resources.MergedDictionaries.Add(ResourceService.GetSharedDictionary(
                new Uri(ResourceHelper.SharedResourcePath, UriKind.Relative)));

            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run();
            bootstrapper.ReLayout();
        }

        private void InitializeCultures()
        {
            if (!string.IsNullOrEmpty(Settings.Default.Culture))
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(Settings.Default.Culture);
            }
            if (!string.IsNullOrEmpty(Settings.Default.UICulture))
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.UICulture);
            }
        }

        private void RunWithExceptionHandling()
        {
            this.DispatcherUnhandledException += AppDispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;

            try
            {
                RunCore();
            }
            catch (Exception ex)
            {
                HandleException(ex, false);
            }
        }

        private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            HandleException(e.Exception, false);
            e.Handled = true;
        }

        private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException(e.ExceptionObject as Exception, e.IsTerminating);
        }

        private static void HandleException(Exception ex, bool isTerminating)
        {
            if (ex == null) { return; }

            ExceptionPolicy.HandleException(ex, "Default Policy");

            if (!isTerminating)
            {
                MessageBox.Show(Jbe.TestSuite.Infrastructure.Shell.Properties.Resources.UnknownError
                    + "\n\n" + ex.ToString()
                    , ApplicationInfo.ProductName, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
